package cn.e3mall.manager.service.impl;

import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.manager.service.ItemCatService;
import cn.e3mall.mapper.TbItemCatMapper;
import cn.e3mall.pojo.TbItemCat;
import cn.e3mall.pojo.TbItemCatExample;
import cn.e3mall.pojo.TbItemCatExample.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Override
    public List<EasyUITreeNode> getItemCat(Long parentId) {
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        Criteria criteria = tbItemCatExample.createCriteria();
        criteria.andParentIdEqualTo( parentId );
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample( tbItemCatExample );
        List<EasyUITreeNode> list = new ArrayList<>(  );
        for (TbItemCat t: tbItemCats) {
            EasyUITreeNode treeNode = new EasyUITreeNode();
            treeNode.setId( t.getId() );
            treeNode.setText( t.getName() );
            treeNode.setState( t.getIsParent()?"closed":"open" );
            list.add( treeNode );
        }
        return list;
    }
}
